import nodePath from "path";
import nodeFileSys from "fs";
import {siteIpList} from "./data-var-list/site-ip-list";
import {domainRootList} from "./data-var-list/domain-root-list";
import {codeSiteList} from "./data-var-list/code-site-list";
import {mostUseSiteList} from "./data-var-list/most-use-site-list";

function getRuleTextFromSiteList(siteList: string[]): string {
    return siteList.map((item) => {
        return "*." + item;
    }).join(sep);
}
const ruleFileDir = nodePath.join(__dirname, "../surge-rule");
const sep = "; ";
let data: string = `
----code site list:[----
${getRuleTextFromSiteList(codeSiteList)}
----]:code site list----

----most use site list:[----
${getRuleTextFromSiteList(mostUseSiteList)}
----]:most use site list----

----domain root:[----
${getRuleTextFromSiteList(domainRootList)}
----]:domain root----

----pure ip:[----
${siteIpList.join(sep)}
----]:pure ip----
`;

nodeFileSys.promises.writeFile(nodePath.join(ruleFileDir, "Proxifier.rule"), data, "utf8").then(() => {
    console.log("write okay");
}).catch((err) => {
    console.error("write error");
    console.error(err);
});
